// 导入解析formdata格式表单数据的包
const multer = require("multer");
// 导入处理路径的核心模块
const path = require("path");
const db = require("../../db/index");
// 创建multer的实例对象,通过dest属性指定文件的存放路径
const upload = multer({
  dest: path.join(__dirname, "../uploads"),
});
const addArticle = (req, res) => {
  console.log(req.body); //文本类型的数据
  console.log("----分割线----");
  console.log(req.file); //文件类型的数据
  if (!req.file || req.file.filedname !== "cover_img")
    return res.cc("文章封面是必选参数");

  const articleInfo = {
    ...req.body,
    cover_img: path.join("/uploads", req.file.filedname),
    pub_date: new Date(),
    author_id: req.user.id,
  };
  const sql = `insert into en_articles set ?`;
  db.query(sql, articleInfo, (err, result) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("发布文章失败！");
    res.cc("发布文章成功", 0);
  });
};

module.exports = {
  addArticle: addArticle,
};
